package com.fuxue.service.impl;

import com.fuxue.config.RandomConfig;
import com.fuxue.constant.MessageConstant;
import com.fuxue.dto.ReceptDTO;
import com.fuxue.mapper.HelpMapper;
import com.fuxue.service.HelpService;
import com.fuxue.vo.ForumVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.login.AccountNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class HelpServiceImpl implements HelpService {

    @Autowired
    private HelpMapper helpMapper;
    @Autowired
    private RandomConfig randomConfig ;
    private final Object updateLock = new Object();
    @Override
    public List<ForumVO> getSelect() {

        List<ForumVO>list = helpMapper.getselect(-1);

        list = randomConfig.selectRandom(list);

        return list;
    }

    @Override
    @Transactional//我注意了线程安全
    public void updateQuestion(ReceptDTO receptDTO) {
           Integer isFinished = helpMapper.getFinished(receptDTO);
        try {
            if(isFinished == 0)
           {

                   throw new AccountNotFoundException(MessageConstant.HAVE_GIVEN);

           } } catch (AccountNotFoundException e) {
            throw new RuntimeException(e);
        }
        synchronized (updateLock) {
            helpMapper.update(receptDTO);
        }
    }
}
